package lib.quasar.widget.edit;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.EditText;
import android.widget.TextView;
import java.lang.reflect.Field;

import lib.quasar.util.LogUtil;
import lib.quasar.widget.R;

@SuppressLint("AppCompatCustomView")
public class FocusEditText extends EditText {

    private int mColorNormal = Color.WHITE;
    private int mColorFocus = Color.GRAY;
    private int mStockWidth = 2;
    private float mStockRadius = 2f;

    private int mTextColorNormal = Color.BLACK;
    private int mTextColorFocus = Color.WHITE;

    private int mHintColorNormal = Color.BLACK;
    private int mHintColorFocus = Color.WHITE;

    private int mStockColorNormal = Color.BLACK;
    private int mStockColorFocus = Color.GRAY;

    private int mCursorColorNormal = Color.BLACK;
    private int mCursorColorFocus = Color.WHITE;

    private boolean needFocus;

    public FocusEditText(Context context) {
        super(context);
        setLongClickable(false);
    }

    public FocusEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        setLongClickable(false);

        TypedArray array = null;
        try {
            array = context.obtainStyledAttributes(attrs, R.styleable.FocusEditText);
            mStockWidth = (int) array.getDimension(R.styleable.FocusEditText_fet_stock_width, mStockWidth);
            mStockRadius = array.getDimension(R.styleable.FocusEditText_fet_stock_radius, mStockRadius);
            mStockColorNormal = array.getColor(R.styleable.FocusEditText_fet_stock_color_normal, mStockColorNormal);
            mStockColorFocus = array.getColor(R.styleable.FocusEditText_fet_stock_color_focus, mStockColorFocus);
            mColorNormal = array.getColor(R.styleable.FocusEditText_fet_background_normal, mColorNormal);
            mColorFocus = array.getColor(R.styleable.FocusEditText_fet_background_focus, mColorFocus);
            mTextColorNormal = array.getColor(R.styleable.FocusEditText_fet_text_color_normal, mTextColorNormal);
            mTextColorFocus = array.getColor(R.styleable.FocusEditText_fet_text_color_focus, mTextColorFocus);
            mHintColorNormal = array.getColor(R.styleable.FocusEditText_fet_hint_color_normal, mHintColorNormal);
            mHintColorFocus = array.getColor(R.styleable.FocusEditText_fet_hint_color_focus, mHintColorFocus);
            mCursorColorNormal = array.getColor(R.styleable.FocusEditText_fet_cursor_color_normal, mCursorColorNormal);
            mCursorColorFocus = array.getColor(R.styleable.FocusEditText_fet_cursor_color_focus, mCursorColorFocus);
        } catch (Exception e) {
            LogUtil.e("", e.getMessage(), e);
        } finally {
            if (null == array) return;
            array.recycle();
        }
    }

    public FocusEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setLongClickable(false);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                needFocus = !needFocus;
                postInvalidate();
                break;
        }
        return super.onTouchEvent(event);
    }

    public void clear() {
        needFocus = false;
        getText().clear();
        clearFocus();
        postInvalidate();
    }

    @Override
    public void draw(Canvas canvas) {

        final GradientDrawable colorDrawable = new GradientDrawable();
        colorDrawable.setColor(needFocus ? mColorFocus : mColorNormal);
        colorDrawable.setStroke(mStockWidth, needFocus ? mStockColorFocus : mStockColorNormal);
        colorDrawable.setCornerRadius(mStockRadius);
        setBackgroundDrawable(colorDrawable);

        setTextColor(needFocus ? mTextColorFocus : mTextColorNormal);
        setHintTextColor(needFocus ? mHintColorFocus : mHintColorNormal);

        super.draw(canvas);

        setCursorColor(needFocus ? mCursorColorFocus : mCursorColorNormal);
    }

    public void setCursorColor(int color) {
        try {
            Field fCursorDrawableRes = TextView.class.getDeclaredField("mCursorDrawableRes");//获取这个字段
            fCursorDrawableRes.setAccessible(true);//代表这个字段、方法等等可以被访问
            int mCursorDrawableRes = fCursorDrawableRes.getInt(FocusEditText.this);

            Field fEditor = TextView.class.getDeclaredField("mEditor");
            fEditor.setAccessible(true);
            Object editor = fEditor.get(FocusEditText.this);

            Class<?> clazz = editor.getClass();
            Field fCursorDrawable = clazz.getDeclaredField("mCursorDrawable");
            fCursorDrawable.setAccessible(true);

            Drawable[] drawables = new Drawable[2];
            drawables[0] = getContext().getResources().getDrawable(mCursorDrawableRes);
            drawables[1] = getContext().getResources().getDrawable(mCursorDrawableRes);
            drawables[0].setColorFilter(color, PorterDuff.Mode.SRC_IN);//SRC_IN 上下层都显示。下层居上显示。
            drawables[1].setColorFilter(color, PorterDuff.Mode.SRC_IN);
            fCursorDrawable.set(editor, drawables);
        } catch (Exception e) {
            LogUtil.e("", e.getMessage(), e);
        }
    }
}
